package org.zachary.imitation.raft;

import java.util.Objects;

/**
 * 对集群中节点的 ip 和 port 的封装
 *
 * @see PeerId
 *
 * Created by guozhaokun on 2024/8/30 11:16.
 */
public class Endpoint {

	/** 节点的 ip */
	private String ip;
	/** 节点的端口 */
	private Integer port;
	/** ip:port */
	private String str;

	public Endpoint(String ip, Integer port) {
		this.ip = ip;
		this.port = port;
	}

	public String getIp() {
		return ip;
	}

	public Integer getPort() {
		return port;
	}

	@Override
	public String toString() {
		if (str == null) {
			str = ip + ":" + port;
		}
		return str;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Endpoint endpoint = (Endpoint) o;
		return Objects.equals(ip, endpoint.ip) && Objects.equals(port, endpoint.port);
	}

	@Override
	public int hashCode() {
		return Objects.hash(ip, port);
	}
}
